﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Web.UI.DataVisualization.Charting;
using Common;
using System.Collections.Generic;
using System.Linq;

namespace CSC440TeamOne.MyHealth.Views
{
    public partial class MyProgress : Microsoft.Practices.CompositeWeb.Web.UI.Page, IMyProgressView
    {
        private MyProgressPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public MyProgressPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	


        public void Show(Common.MyProgressViewModel model)
        {
            IEnumerable<TrackableHealthParameter> trackableHealthParameters = model.TrackableHealthParameters;

            if (trackableHealthParameters.Any())
            {
                mvState.SetActiveView(vwProgress);

                var series = new Series("Weight Progress");

                series.ChartType = SeriesChartType.Line;

                foreach (var trackableHealthParameter in trackableHealthParameters)
                {
                    DateTime dateTime = trackableHealthParameter.EnteredOn;

                    double value = trackableHealthParameter.Value;

                    DataPoint dataPoint = new DataPoint(dateTime.ToOADate(), value);

                    series.Points.Add(value);
                }

                chtProgress.Series.Add(series);

                if (model.HasMaxTolerance)
                {
                    series = new Series("Max Tolerrance");

                    series.ChartType = SeriesChartType.Line;

                    series.BorderWidth = 5;

                    series.BorderDashStyle = ChartDashStyle.Dot;

                    foreach (var trackableHealthParameter in trackableHealthParameters)
                    {
                        DateTime dateTime = trackableHealthParameter.EnteredOn;

                        double maxValue = trackableHealthParameter.MaxValue;

                        DataPoint dataPoint = new DataPoint(dateTime.ToOADate(), maxValue);

                        series.Points.Add(maxValue);
                    }

                    chtProgress.Series.Add(series);
                }

                if (model.HasMinTolerance)
                {
                    series = new Series("Min Tolerrance");

                    series.ChartType = SeriesChartType.Line;

                    series.BorderWidth = 5;

                    series.BorderDashStyle = ChartDashStyle.Dot;

                    foreach (var trackableHealthParameter in trackableHealthParameters)
                    {
                        DateTime dateTime = trackableHealthParameter.EnteredOn;

                        double minValue = trackableHealthParameter.MinValue;

                        DataPoint dataPoint = new DataPoint(dateTime.ToOADate(), minValue);

                        series.Points.Add(minValue);
                    }

                    chtProgress.Series.Add(series);
                }
            }
        }
    }
}

